package wkday12.demo02;

public class ChiHuo extends Thread {
    private final BaoZi bz;

    ChiHuo(String name, BaoZi bz) {
        super(name);
        this.bz = bz;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (bz) {
                // 没包子
                if (!bz.flag) {
                    try {
                        // 等待包子做好
                        System.out.println(Thread.currentThread().getName() + " 等待包子出笼");
                        bz.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    // 有包子
                    System.out.println(Thread.currentThread().getName() + " 正在吃 " + bz.pi + bz.xian + " 包子");
                    // 吃掉包子
                    bz.flag = false;
                    // 唤醒
                    bz.notifyAll();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
